package com.gupao.gupaoeduvippattern.javaBase.thread.threadLocal;

import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

/**
 * @author Administrator
 * @date 2020-12-08 10:44
 */
public class ThreadLocalTest01 {

    public static void main(String[] args) {
        //新建ThreadLocal
        ThreadLocal<String> local = new ThreadLocal<>();
        //新建一个随机数类
        Random random = new Random();
        //使用java8的stream新建5个线程
        IntStream.range(0, 5).forEach(a->new Thread(()->{
            //为每个线程设置相应的Local值
            local.set(a +","+ random.nextInt(100));
            System.out.println("线程和Local值分别是：" + local.get());

            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


        }).start());



    }

}
